﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using Telerik.Web.UI;
using KPIS.Utils;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_RoyalItemRequests : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private IRoyalItemRequestBLL reqBLL = new RoyalItemRequestBLL();
        private IEmployeeBLL empBLL = new EmployeeBLL();
        private IPositionBLL posBLL = new PositionBLL();

        private EmployeeInfo empInfo = new EmployeeInfo();
        private RadComboBox cmbYear, cmbPosition, cmbClass, cmbJobSite, cmbReqRoyalItem, cmbCurrRoyalItem;
        private object _dataItem = null;        
        protected void Page_Load(object sender, EventArgs e)
        {
            cmbPosition = (RadComboBox)cbPosition.FindControl("cbPosition");
            cmbYear = (RadComboBox)cbYear.FindControl("cbYear");
            cmbClass = (RadComboBox)cbClass.FindControl("cbClass");
            cmbJobSite = (RadComboBox)cbJobSite.FindControl("cbJobSite");
            cmbReqRoyalItem = (RadComboBox)cbReqRoyalItem.FindControl("cbRoyalItem");
            cmbCurrRoyalItem = (RadComboBox)cbCurrRoyalItem.FindControl("cbRoyalItem");

            if (DataItem != null)
            {
                cbPosition.ListPosition("", Convert.ToString(Session["EMPTYPE"]));
                cbYear.ListYear();
                cbClass.ListClassComboBox("");
                cbJobSite.ListJobSiteComboBox(SystemInfo._JobsiteExternal);
                cbReqRoyalItem.ListRoyalItemComboBox();
                cbCurrRoyalItem.ListRoyalItemComboBox();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblSequence.Text = DataBinder.Eval(DataItem, "Sequence").ToString();
                    cmbYear.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "RequestYear"));
                    cmbReqRoyalItem.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "RoyalItem.Sequence"));
                    FillDataToEmployees();
                    cmbEmployee.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "Employee.Sequence"));
                    cmbPosition.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "Position.Sequence"));
                    cmbClass.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "Class.Sequence"));
                    cmbJobSite.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "JobSite.Sequence"));
                    cmbCurrRoyalItem.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "CurrentRoyalItem.Sequence"));
                    txtSalary.Text = DataBinder.Eval(DataItem, "Salary").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "Remarks").ToString();
                    chkNeed.Checked = Convert.ToString(DataBinder.Eval(DataItem, "Need")) == "1";
                }
                else
                {
                    cmbYear.SelectedValue = Convert.ToString(Int32.Parse(DateTime.Today.Year.ToString()) + 543);
                }
                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void FillDataToEmployees()
        {
            IList<EmployeeInfo> infoList = empBLL.ListEmployeeByTypes(Convert.ToString(Session["EMPTYPE"]) , "", ""); 

            foreach (EmployeeInfo List in infoList)
            {
                RadComboBoxItem item = new RadComboBoxItem();

                item.Text = List.Person.FullName;//  List.Person.NamePrefix.Name + List.Person.FirstName + " " + List.Person.LastName; --> error because nameprefix is null
                item.Value = List.Sequence.ToString();

                item.Attributes.Add("EMP_NAME", List.Person.FullName);
                item.Attributes.Add("POS_NAME", List.Position == null ? "" : List.Position.Name);
                item.Attributes.Add("CLASS_NAME", List.Class == null ? "" : List.Class.Name);

                cmbEmployee.Items.Add(item);
                item.DataBind();
            }

        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            reqBLL.Insert(Int32.Parse(cmbYear.SelectedValue)
                , cmbEmployee.SelectedValue
                , cmbPosition.SelectedValue
                , cmbClass.SelectedValue
                , cmbJobSite.SelectedValue
                , double.Parse(txtSalary.Text.Trim())
                , cmbReqRoyalItem.SelectedValue
                , cmbCurrRoyalItem.SelectedValue
                , chkNeed.Checked
                , txtRemarks.Text.Trim()
                , ssMng.EmpSeqI
                );
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            reqBLL.Update(Int32.Parse(lblSequence.Text.Trim())
                , Int32.Parse(cmbYear.SelectedValue)
                , cmbEmployee.SelectedValue
                , cmbPosition.SelectedValue
                , cmbClass.SelectedValue
                , cmbJobSite.SelectedValue
                , double.Parse(txtSalary.Text.Trim())
                , cmbReqRoyalItem.SelectedValue
                , cmbCurrRoyalItem.SelectedValue
                , chkNeed.Checked
                , txtRemarks.Text.Trim()
                , ssMng.EmpSeqI
                );
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void cmbEmployee_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            IList<EmployeeInfo> empList = empBLL.ListEmployees(cmbEmployee.SelectedValue);
            if (empList.Count > 0)
            {
                empInfo = empList[0];

                if (empInfo.Position != null)
                    cmbPosition.SelectedValue = empInfo.Position.Sequence.ToString();
                if (empInfo.Class != null)
                    cmbClass.SelectedValue = empInfo.Class.Sequence.ToString();
                if (empInfo.Position != null)
                    if (empInfo.Position.JobSite != null)
                        cmbJobSite.SelectedValue = empInfo.Position.JobSite.Sequence.ToString();
                txtSalary.Text = string.Format("{0:N1}", empInfo.Salary);
            }
        }

        protected void cmbEmployee_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            FillDataToEmployees();
        }

    }
}